﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class cms_Admin_Danhmuc_u_Quanlychucvu : System.Web.UI.UserControl
{
    #region[Declare varian]
    static string ID = "";
    static string insert = "";
    static string Level = "";
    #endregion
    #region[Page_Load]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { Viewlist(); Showthutu(); }
    }
    #endregion
    #region[Showthutu]
    void Showthutu()
    {
        drlThutu.Items.Clear();
        for (int i = 1; i < 51; i++)
        {
            drlThutu.Items.Add(i.ToString());
        }
    }
    #endregion
    #region[Viewlist]
    void Viewlist()
    {
        List<tbPositionsDATA> list = new List<tbPositionsDATA>();
        list = tbPositionsDB.tbPositions_GetByAll();
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
        }
    }
    #endregion
    #region[Add]
    protected void lbtinsertadv_Click(object sender, EventArgs e)
    {
        Viewlist();
        pnDanhsach.Visible = false;
        pnUpdate.Visible = true;
        Resetcontrol();
        insert = "true";
    }
    #endregion
    #region[Delete]
    protected void Delete(object sender, System.EventArgs e)
    {
        ((LinkButton)sender).Attributes["onclick"] = "return confirm('Bạn chắc chắn muốn xóa thông tin vừa chọn !!') ";
    }
    #endregion
    #region[Update]
    protected void lbtCapnhat_Click(object sender, EventArgs e)
    {
        if (Test() == true)
        {
            if (insert == "true")
            {
                tbPositionsDATA ObjtbPositionsDATA = new tbPositionsDATA();
                ObjtbPositionsDATA.psCode = txtMa.Text;
                ObjtbPositionsDATA.psName = txtTen.Text;
                if (Level.Length > 0)
                {
                    ObjtbPositionsDATA.psLevel = Level + "00000";
                }
                else
                {
                    ObjtbPositionsDATA.psLevel = Level;
                }
                ObjtbPositionsDATA.psOrd = drlThutu.SelectedValue;
                try
                {
                    if (tbPositionsDB.tbPositions_Add(ObjtbPositionsDATA))
                    {
                        lblThongbao.Text = "Thêm mới chức vụ thành công !!";
                        Resetcontrol();
                        insert = "";
                        Viewlist();
                        pnDanhsach.Visible = true;
                        pnUpdate.Visible = false;
                    }
                }
                catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
            }
            else
            {
                if (ID.Length > 0)
                {
                    List<tbPositionsDATA> list = tbPositionsDB.tbPositions_GetByID(ID);
                    if (list.Count > 0)
                    {
                        list[0].psID = ID;
                        list[0].psCode = txtMa.Text;
                        list[0].psName = txtTen.Text;
                        list[0].psLevel = Level;
                        list[0].psOrd = drlThutu.SelectedValue;
                        try
                        {
                            if (tbPositionsDB.tbPositions_Update(list[0]))
                            {
                                lblThongbao.Text = "Cập nhật chức vụ thành công !!";
                                Resetcontrol();
                                insert = "";
                                Viewlist();
                                pnDanhsach.Visible = true;
                                pnUpdate.Visible = false;
                            }
                        }
                        catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
                    }
                }
            }
        }
    }
    #endregion
    #region[Back]
    protected void lbtTrolai_Click(object sender, EventArgs e)
    {
        Viewlist();
        pnDanhsach.Visible = true;
        pnUpdate.Visible = false;
        insert = "";
        Resetcontrol();
    }
    #endregion
    #region[Resetcontrol]
    void Resetcontrol()
    {
        txtMa.Text = "";
        txtTen.Text = "";
        drlThutu.SelectedValue = "1";
        Level = "";
    }
    #endregion
    #region[Test]
    protected bool Test()
    {
        if (txtMa.Text == "") { lblLoi.Text = "Chưa nhập mã chức vụ !!"; return false; }
        if (txtTen.Text == "" && insert == "true") { lblLoi.Text = "Chưa nhập tên chức vụ !!"; return false; }
        return true;
    }
    #endregion
    #region[rptDanhsach_ItemCommand]
    protected void rptDanhsach_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string ten = e.CommandName.ToString();
        ID = e.CommandArgument.ToString();
        switch (ten)
        {
            case "Edit":
                insert = "false";
                List<tbPositionsDATA> list = tbPositionsDB.tbPositions_GetByID(ID);
                txtMa.Text = list[0].psCode;
                txtTen.Text = list[0].psName;
                Level = list[0].psLevel;
                drlThutu.SelectedValue = list[0].psOrd;
                pnDanhsach.Visible = false;
                pnUpdate.Visible = true;
                break;
            case "Delete":
                tbPositionsDB.tbPositions_Delete(ID);
                lblThongbao.Text = "Xóa thành công chức vụ !!";
                Viewlist();
                break;
            case "Add":
                List<tbPositionsDATA> lists = tbPositionsDB.tbPositions_GetByID(ID);
                insert = "true";
                Resetcontrol();
                Level = lists[0].psLevel;
                pnDanhsach.Visible = false;
                pnUpdate.Visible = true;
                break;
        }
    }
    #endregion
    #region[Hienthihinhcay]
    protected string Hienthihinhcay(string name, string treecode)
    {
        string chuoi = "<img src='images/admin/Speerio_folderopen_edit.gif' border=0>";
        if (treecode.ToString().Trim().Length > 50)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 45)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 40)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 35)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 30)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 25)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 20)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 15)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 10)
        {
            chuoi = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else if (treecode.ToString().Trim().Length > 5)
        {
            chuoi = "&nbsp;&nbsp;<img src='images/admin/sub.gif' border=0>";
        }
        else
        {
            chuoi = chuoi;
        }
        return chuoi = chuoi + "&nbsp;" + name;
    }
    #endregion
}
